<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="水票码名称" prop="codeName">
        <el-input
          v-model="queryParams.codeName"
          placeholder="请输入水票码名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="水票ID" prop="waterBillId">
        <el-input
          v-model="queryParams.waterBillId"
          placeholder="请输入水票ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平台渠道" prop="platformSource">
        <el-select v-model="queryParams.platformSource" placeholder="请选择平台渠道" style="width: 150px;" clearable>
          <el-option
            v-for="dict in platform_source_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" style="width: 150px;" clearable>
          <el-option
            v-for="dict in water_bill_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:waterbillcode:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:waterbillcode:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:waterbillcode:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:waterbillcode:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="waterbillcodeList" border @selection-change="handleSelectionChange">
      <el-table-column type="index" :index="indexMethod" />
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="水票码名称" align="center" prop="codeName" />
      <el-table-column label="生成数量" align="center" prop="generateNum" />
      <el-table-column label="密码位数" align="center" prop="passwordDigit" />
      <el-table-column label="平台渠道" align="center" prop="platformSource">
        <template #default="scope">
          <dict-tag :options="platform_source_type" :value="scope.row.platformSource"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="beginTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.beginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="water_bill_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="商品" align="center" prop="title" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:waterbillcode:edit']">修改</el-button> -->
          <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:waterbillcode:remove']">删除</el-button> -->
          <el-button link type="primary" icon="Search" @click="searchDetail(scope.row)" v-hasPermi="['business:waterbillcode:list']">查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改水票码信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="waterbillcodeRef" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="水票码名称" prop="codeName">
          <el-input v-model="form.codeName" placeholder="请输入水票码名称" />
        </el-form-item>
        <el-form-item label="关联水票" prop="waterBillId">
          <el-select v-model="form.waterBillId" placeholder="请选择">
            <el-option v-for="item in waterBillOptions" :key="item.id" :label="item.title" :value="item.id" :disabled="item.status == 1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生成数量" prop="generateNum">
          <el-input v-model="form.generateNum" placeholder="请输入生成数量" />
        </el-form-item>
        <el-form-item label="密码位数" prop="passwordDigit">
          <el-input v-model="form.passwordDigit" placeholder="请输入密码位数" />
        </el-form-item>
        <el-form-item label="平台渠道" prop="platformSource">
          <el-select v-model="form.platformSource" placeholder="请选择平台渠道">
            <el-option
              v-for="dict in platform_source_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="有效期" prop="validTimeRange">
          <el-date-picker clearable
            v-model="form.validTimeRange"
            type="daterange"
            value-format="YYYY-MM-DD"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="截止时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog :title="detailTitle" v-model="detailOpen" width="80%" append-to-body>
      <Waterbillcodedetail :codeId="selectedCodeId"></Waterbillcodedetail>
    </el-dialog>
  </div>
</template>

<script setup name="Waterbillcode">
import { listWaterbillcode, getWaterbillcode, delWaterbillcode, addWaterbillcode, updateWaterbillcode } from "@/api/business/waterbillcode"
import { ref } from "vue"
import Waterbillcodedetail from "../waterbillcodedetail/index"

const { proxy } = getCurrentInstance()
const { platform_source_type, water_bill_status } = proxy.useDict('platform_source_type', 'water_bill_status')

const waterbillcodeList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")
const waterBillOptions = ref([])
const detailOpen = ref(false)
const detailTitle = ref("")
const selectedCodeId = ref(null)

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    codeName: null,
    waterBillId: null,
    platformSource: null,
    status: null,
  },
  rules: {
    codeName: [{require: true, message: "水票码名称不能为空", trigger: "blur"}],
    waterBillId: [{ required: true, message: "关联水票不能为空", trigger: "blur" }],
    generateNum: [{ required: true, message: "生成数量不能为空", trigger: "blur" }],
    passwordDigit: [{ required: true, message: "密码位数不能为空", trigger: "blur" }],
    platformSource: [{ required: true, message: "平台渠道不能为空", trigger: "blur" }],
    validTimeRange: [{ required: true, message: "有效期不能为空", trigger: "blur" }]
  }
})

const { queryParams, form, rules } = toRefs(data)

// 列表行号
const indexMethod = (index) => {
  return index + 1
}

/** 查询水票码信息列表 */
function getList() {
  loading.value = true
  listWaterbillcode(queryParams.value).then(response => {
    waterbillcodeList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    codeName: null,
    waterBillId: null,
    generateNum: null,
    passwordDigit: null,
    platformSource: null,
    beginTime: null,
    endTime: null,
    status: null,
    commodityId: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    validTimeRange: ''
  }
  proxy.resetForm("waterbillcodeRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  getWaterbillcode().then(response => {
    waterBillOptions.value = response.waterBills
    open.value = true
    title.value = "添加水票码信息"
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getWaterbillcode(_id).then(response => {
    form.value = response.data
    // 有效期
    form.value.validTimeRange = [form.value.beginTime, form.value.endTime]
    waterBillOptions.value = response.waterBills
    open.value = true
    title.value = "修改水票码信息"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["waterbillcodeRef"].validate(valid => {
    if (valid) {
      // 开始时间
      form.value.beginTime = form.value.validTimeRange[0]
      // 截止时间
      form.value.endTime = form.value.validTimeRange[1]

      if (form.value.id != null) {
        updateWaterbillcode(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addWaterbillcode(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除水票码信息编号为"' + _ids + '"的数据项？').then(function() {
    return delWaterbillcode(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/waterbillcode/export', {
    ...queryParams.value
  }, `waterbillcode_${new Date().getTime()}.xlsx`)
}

/**
 * 查看详情
 * @param row 
 */
function searchDetail(row) {
    detailOpen.value = true
    detailTitle.value = "水票码详情"
    selectedCodeId.value = row.id
}

getList()
</script>
